package hongcaosp.app.android.comment;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.modle.bean.IComment;
import hongcaosp.app.android.modle.bean.IUser;
import xlj.lib.android.base.utils.GlideUtils;
import xlj.lib.android.base.view.CircleImageView;
import xlj.lib.android.base.view.OnRecycleItemClickListener;

public class CommentHolder extends BaseHolder<IComment> implements GoodFreshProxy {
    private Context context;
    private ImageView userLogo;
    private TextView userNameTV,commentTV,goodTV;

    private CommentGoodListener commentGoodListner;

    public void setCommentGoodListner(CommentGoodListener commentGoodListner) {
        this.commentGoodListner = commentGoodListner;
    }

    public CommentHolder(Context context, View itemView, OnRecycleItemClickListener recycleItemClickListener) {
        super(itemView,recycleItemClickListener);
        this.context = context;
        userLogo = itemView.findViewById(R.id.user_logo);
        userNameTV = itemView.findViewById(R.id.user_name);
        commentTV = itemView.findViewById(R.id.comment);
        goodTV = itemView.findViewById(R.id.good_count);

        goodTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getData() != null) {
                    if (getData().isLike()) {
                        if (commentGoodListner != null) {
                            commentGoodListner.onCancelGood(getData(),CommentHolder.this);
                        }
                    } else {
                        if (commentGoodListner != null) {
                            commentGoodListner.onGood(getData(),CommentHolder.this);
                        }
                    }
                }
            }
        });
    }

    @Override
    public void setData(IComment iComment) {
        super.setData(iComment);
        IUser user = iComment.user();
        if (user != null) {
            GlideUtils.loadCircleImage(context,user.getUserLogo(),userLogo,R.drawable.icon_head);
            userNameTV.setText(user.getUserName());
        }
        commentTV.setText(iComment.comment());
        SpannableString spannableString = new SpannableString(iComment.commentTime());
        spannableString.setSpan(new TextAppearanceSpan(context,R.style.CommentTimeStyle),0,spannableString.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        commentTV.append(spannableString);

        freshGoodState();
    }

    @Override
    public void freshGoodState() {
        IComment comment = getData();
        if (comment != null) {
            goodTV.setText(comment.getLikeCounts() + "");
            goodTV.setSelected(comment.isLike());
        }
    }
}
